import java.time.LocalDateTime;
import java.util.*;
public class RecordManager implements RecordOperable
{
    private Map<RecordType,List<BaseRecord>>recordMap;
    private Set<String>recordIdSet;
    public RecordManager(){
        recordMap=new HashMap<>();
        recordIdSet=new HashSet<>();
    }
    @Override
    public boolean addRecord(BaseRecord record){
        String recordId=record.getRecordId();
        if (!recordIdSet.add(recordId)){
            return false;
        }
        RecordType type=record.getType();
        recordMap.computeIfAbsent(type,k->new ArrayList<>());
        List<BaseRecord> typeRecords = getRecords(type);
        typeRecords.add(record);
        sortRecordsByCreateTimeDesc(type);
                return true;
    }

    private List<BaseRecord> getRecords(RecordType type) {
        List<BaseRecord>typeRecords=recordMap.get(type);
        return typeRecords;
    }

    public void sortRecordsByCreateTimeDesc(RecordType type){
        List<BaseRecord>records=recordMap.get(type);
        if(records==null||records.isEmpty()){
            return;
        }
        records.sort((r1,r2)->{
            return r2.getCreatime().compareTo(r1.getCreatime());
        });
}
public List<BaseRecord> getRecordsByType(RecordType type){
        return recordMap.getOrDefault(type,new ArrayList<>());

}

}
